<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.example.pojo.Student" %>
<%@ page import="com.example.dao.StudentMapper" %>
<%@ page import="com.example.utils.MybatisUtils" %>
<%@ page import="org.apache.ibatis.session.SqlSession" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.pojo.StuClass" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        h1 {
            text-align: center;
        }

        table {
            width: 80%;
            margin: 20px auto;
            border-collapse: collapse;
            background-color: #fff;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }

        th {
            background-color: #f2f2f2;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #ddd;
        }

        .btn-group {
            display: inline-block;
        }

        .btn-group button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 8px 16px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 14px;
            margin: 2px 2px;
            cursor: pointer;
        }

        .btn-group button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<h1>学生信息</h1>
<!-- 查询和添加学生按钮 -->
<div class="btn-group">
    <a href="search.jsp"><button>查询学生</button></a>
    <a href="add.jsp"><button>添加学生</button></a>
</div>
<!-- 学生信息表格 -->
<table>
    <thead>
    <tr>
        <th>姓名</th>
        <th>学号</th>
        <th>班级</th>
        <th>院系</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <%
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getAllStudents();
        SqlSession sqlSession1 = MybatisUtils.getSqlSession();
        StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
        for(Student student : studentList) {
            StuClass stuClass = mapper1.getClassByClassId(student.getClass_id());
    %>
    <tr>
        <td><%= student.getStu_name() %></td>
        <td><%= student.getStu_no() %></td>
        <td><%= stuClass.getClass_name() %></td>
        <td><%= stuClass.getDepartment() %></td>
        <td>
            <!-- 修改和删除按钮 -->
            <div class="btn-group">
                <form id="updateForm" action="update_student.jsp" method="get">
                    <input type="hidden" id="stu_id1" name="id" value="<%= student.getStu_id() %>">
                    <button type="submit">修改</button>
                </form>
                <form id="deleteForm" action="delete_student.jsp" method="get">
                    <input type="hidden" id="stu_id" name="stu_id" value="<%= student.getStu_id() %>">
                    <button type="submit">删除</button>
                </form>
            </div>
        </td>
    </tr>
    <%
        }
        sqlSession.close(); // 关闭SqlSession
    %>
    </tbody>
</table>
</body>
</html>
